//
//  RateCenter.m
//  TravelGuideMdd
//
//  Created by BigPolarBear on 14-6-20.
//  Copyright (c) 2014年 mafengwo.com. All rights reserved.
//

#import "RateCenter.h"
#import "Appirater.h"

#define kUserDefaultsKey_launch_number_by_version   @"launch_number_by_version"
#define kUserDefaultsKey_launch_date_by_version     @"launch_date_by_version"
#define kUserDefaultsKey_rate_by_version            @"rate_by_version"

// 当前版本的启动次数
#define AppLaunchNumberNeedRate 3
// 当前版本距离首次启动的天数
#define MaxDay  1

@implementation RateCenter

/** 会根据启动次数，决定是否需要提示用户评价 */
+(void)appLaunched
{
    [self currentVersionLaunchNumberIncrement];
}

+(BOOL)currentVersionRated
{
    NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:kUserDefaultsKey_rate_by_version]];
    return [[dict objectForKey:[self currentVersion]] boolValue];
}

/** 是否需要评价 */
+(BOOL)needRate
{
    NSDate* launchDate = [self currentVersionFirstLaunchDate];
    
    // 当前版本未评价过 && 启动次数达到阈值 && 启动时间超过天数
    if (![self currentVersionRated]
        && [self currentVersionLaunchNumber] >= AppLaunchNumberNeedRate
        && launchDate
        && [[NSDate date] timeIntervalSinceDate:launchDate] > 3600*24*MaxDay
        ) {
        return YES;
    }else{
        return NO;
    }
}

/** 去评价 */
+(void)gotoRate
{
    [self setRated];
    [Appirater rateApp];
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationUserRated object:nil];
}

#pragma mark 辅助方法
+(NSString*)currentVersion
{
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}

+(NSInteger)currentVersionLaunchNumber
{
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSString* version = [self currentVersion];
    NSDictionary* dict = [defaults objectForKey:kUserDefaultsKey_launch_number_by_version];
    NSNumber* launchNumber = [dict objectForKey:version];
    return [launchNumber integerValue];
}
+(void)currentVersionLaunchNumberIncrement
{
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSString* version = [self currentVersion];
    NSDictionary* dict = [defaults objectForKey:kUserDefaultsKey_launch_number_by_version];
    NSMutableDictionary* mutableDict = [NSMutableDictionary dictionaryWithDictionary:dict];
    
    
    NSNumber* launchNumber = [mutableDict objectForKey:version];
    if (!launchNumber) {
        NSMutableDictionary* dictDate = [NSMutableDictionary dictionaryWithDictionary:[defaults objectForKey:kUserDefaultsKey_launch_date_by_version]];
        [dictDate setObject:[NSDate date] forKey:version];
        [defaults setObject:dictDate forKey:kUserDefaultsKey_launch_date_by_version];
    }
    
    [mutableDict setObject:[NSNumber numberWithInt:([self currentVersionLaunchNumber]+ 1)] forKey:version];
    
    [[NSUserDefaults standardUserDefaults] setObject:mutableDict forKey:kUserDefaultsKey_launch_number_by_version];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(NSDate*)currentVersionFirstLaunchDate
{
    NSDictionary* dict = [[NSUserDefaults standardUserDefaults] objectForKey:kUserDefaultsKey_launch_date_by_version];
    NSDate* launchDate = [dict objectForKey:[self currentVersion]];
    return launchDate;
}

+(void)setRated
{
    NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:kUserDefaultsKey_rate_by_version]];
    [dict setObject:[NSNumber numberWithBool:YES] forKey:[self currentVersion]];
    
    [[NSUserDefaults standardUserDefaults] setObject:dict forKey:kUserDefaultsKey_rate_by_version];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
